package com.knight.controller;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.knight.common.constant.Constant;
import com.knight.exception.RequestParameterException;
import com.knight.model.Content;
import com.knight.model.Menu;
import com.knight.service.ContentService;
import com.knight.service.MenuService;

public class NewsController extends Controller {
	private static MenuService menuService = MenuService.getInstance();
	private static ContentService contentService = ContentService.getInstance();

	private static List<Menu> list = menuService.findMenuByparentId(Constant.MenuTypes.LEVEL_TWO_MENU,
			Constant.ParentMenuId.NEWS_ID);

	/**
	 * 公司动态
	 * 
	 * @throws Exception
	 */
	public void contentDetail() throws Exception {
		String requestData = getPara();
		Map<String, String> map = commonMethod(requestData, this);
		Content c = contentService.findById(map.get("id"));
		setAttr("c", c);
		render("news/news_content.html");
	}
	
	
	/**
	 * 公共方法
	 * 
	 * @param list
	 * @param requestData
	 * @param controller
	 * @return
	 * @throws Exception
	 */
	private Map<String, String> commonMethod(String requestData, Controller controller)
			throws Exception {
		if (list == null || list.size() == 0) {
			throw new RequestParameterException(Constant.ExceptionMessage.PARAMETER_IS_NULL);
		}
		Menu firstMenu = list.get(0);
		String id = null;
		if (requestData != null) {
			id = controller.getPara(0);
		} else {
			firstMenu.getParentId();
			id = firstMenu.get("id").toString();
		}
		controller.setAttr("menus", list);
		Menu m = menuService.findByPrimaryKey(firstMenu.getParentId());
		controller.setAttr("secondMenuName", m.getName());

		Map<String, String> map = new HashMap<String, String>();

		map.put("id", id);

		return map;
	}

	/**
	 * 项目动态
	 */
	public void projectNews() throws Exception {
		String name = URLDecoder.decode(getPara(), "UTF-8");
		setAttr("menus", list);
		setAttr("secondMenuName", name);
		render("news/news.html");
	}
}
